package cn.itcast.educenter.controller;


import cn.itcast.commonutils.JwtUtils;
import cn.itcast.commonutils.R;
import cn.itcast.commonutils.ordervo.UCenterMemberOrder;
import cn.itcast.educenter.entity.UcenterMember;
import cn.itcast.educenter.entity.vo.RegisterVo;
import cn.itcast.educenter.service.UcenterMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author Duanzihuang
 * @since 2020-06-12
 */
@RestController
@RequestMapping("/educenter/member")
@CrossOrigin
@Api(description = "登录管理")
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService memberService;

    // 登录
    @ApiOperation(value="登录")
    @PostMapping("login")
    public R login(@RequestBody UcenterMember member){
        // 调用service方法实现登录
        String token = memberService.login(member);

        // 返回token值，使用jwt生成
        return R.ok().data("token",token);
    }

    // 注册
    @ApiOperation(value="注册")
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return R.ok();
    }

    // 根据token获取用户信息
    @GetMapping("getMemberInfo")
    @ApiOperation(value="根据token获取用户信息")
    public R getMemberInfo(HttpServletRequest request) {
        // 调用jwt工具类的方法，根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        // 查询数据库根据用户id获取用户信息
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("userInfo",member);
    }

    // 根据用户id，获取用户信息
    @ApiOperation(value = "根据用户id获取用户信息")
    @PostMapping("/getMemberInfo/{memberId}")
    public UCenterMemberOrder getMemberInfo(@PathVariable String memberId){
        UcenterMember member = memberService.getById(memberId);

        UCenterMemberOrder memberOrder = new UCenterMemberOrder();

        // copy属性
        BeanUtils.copyProperties(member,memberOrder);

        return memberOrder;
    }

    // 获取每天的注册人数
    @ApiOperation(value = "获取每天注册的人数")
    @GetMapping("/getRegisterCount/{day}")
    public R getRegisterCount(@PathVariable String day){
        int count = memberService.getRegisterCount(day);
        return R.ok().data("registerCount",count);
    }
}

